package ocCaptcha

import (
	"encoding/base64"
	"github.com/mojocn/base64Captcha"
	"strings"
)

var store = base64Captcha.DefaultMemStore

// 生成图形验证码
// len验证码字数
// width宽度
// height高度
// return id
func GetCaptcha(len, width, height int) (id string, imageBuffer []byte, answer string, err error) {
	driverString := &base64Captcha.DriverString{
		Height:          height,
		Width:           width,
		NoiseCount:      3,
		ShowLineOptions: 1,
		Length:          len,
		Source:          "3456789qwertyuopasdfghjkzxcvbnmQWERTYUOPASDFGHJKZXCVBNM",
		Fonts:           nil,
	}
	driver := driverString.ConvertFonts()
	c := base64Captcha.NewCaptcha(driver, store)
	id, b64s, answer, err := c.Generate()
	if err != nil {
		return "", nil, "", err
	}
	//	剪切图片字符串
	decodeParts := strings.Split(b64s, ",")
	imageBuffers, err := base64.StdEncoding.DecodeString(decodeParts[1])
	if err != nil {
		return "", nil, "", err
	}
	return id, imageBuffers, answer, nil

}
